{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "40157b48",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 锂离子电池生产参数调控及生产温度预测挑战赛 \n",
    "### Python 语法解析专题\n",
    "<p></p>\n",
    "\n",
    "#### Datawhale 7 月百校万人实战组队学习活动\n",
    "\n",
    "**高校联盟 聪明办法学 Python 教学团队 X Datawhale 竞赛俱乐部**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fee56047",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9725717a",
   "metadata": {},
   "source": [
    "<p>骆秀韬 epsilon_luoo@outlook.com</p>\n",
    "\n",
    "聪明办法学 Python 教学团队"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6951a280",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# print( ) 与 注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76135e51",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "第一行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1ad1d911",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-17T04:41:02.489867Z",
     "start_time": "2023-07-17T04:41:02.473791Z"
    },
    "run_control": {
     "marked": false
    },
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello, world\") # f(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7370d7b8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "**单行注释** 使用 `#` 开头，`#` 后面的内容不会被当做代码，只能写在一行中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "578e7867",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:05:31.090187Z",
     "start_time": "2023-07-22T12:05:31.084188Z"
    },
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Datawhale\n"
     ]
    }
   ],
   "source": [
    "print(\"Datawhale\") # for the learner，和学习者一起成长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "0911a0cd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:06:52.727969Z",
     "start_time": "2023-07-22T12:06:52.710970Z"
    },
    "run_control": {
     "marked": false
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p2s\n"
     ]
    }
   ],
   "source": [
    "# learn python the smart way v2\n",
    "print(\"p2s\")\n",
    "# print(\"prepare to be smart\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a29d72e",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "每一个 `print()` 会默认换行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "0d566b73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:07:09.730454Z",
     "start_time": "2023-07-22T12:07:09.721453Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data\n",
      "whale\n"
     ]
    }
   ],
   "source": [
    "print(\"Data\")\n",
    "print(\"whale\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "7dfa7029",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:07:41.621544Z",
     "start_time": "2023-07-22T12:07:41.610546Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Datawhale\n"
     ]
    }
   ],
   "source": [
    "print(\"Data\", end=\"\")\n",
    "print(\"whale\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af768399",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "多个内容是以**空格**分隔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d040a1f9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:08:03.577742Z",
     "start_time": "2023-07-22T12:08:03.568741Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data whale\n"
     ]
    }
   ],
   "source": [
    "print(\"Data\", \"whale\") # f(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6e69a6e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:08:43.761359Z",
     "start_time": "2023-07-22T12:08:43.754359Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data*whale\n"
     ]
    }
   ],
   "source": [
    "print(\"Data\", \"whale\", sep=\"*\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9db6fc2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 列表与字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "672fbe08",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "**列表是一种序列**，它是**可变**的\n",
    "\n",
    "> 容器的唯一作用就是打包、解包、内容传递。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "fcc53480",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:11:57.439544Z",
     "start_time": "2023-07-22T12:11:57.422543Z"
    },
    "run_control": {
     "marked": false
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['learn', 'python', 'the', 'smart', 'way']\n"
     ]
    }
   ],
   "source": [
    "p2s = [\"learn\", \"python\", \"the\", \"smart\", \"way\"] # f(x, y)\n",
    "print(p2s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "dc687c7c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:12:26.574637Z",
     "start_time": "2023-07-22T12:12:26.565639Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python learn way smart\n"
     ]
    }
   ],
   "source": [
    "print(p2s[1], p2s[0], p2s[-1], p2s[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "57cb6d1d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:14:11.616193Z",
     "start_time": "2023-07-22T12:14:11.599682Z"
    },
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['learn', 'python']\n",
      "['the', 'smart', 'way']\n"
     ]
    }
   ],
   "source": [
    "print(p2s[0:2]) # 切片 左闭右开区间\n",
    "print(p2s[2:]) # 起始：0，结束：-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2de59561",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "字典是**键值对**的集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9d2f2a6b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:16:42.076137Z",
     "start_time": "2023-07-22T12:16:42.068136Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a', 'D', 'w', 'l', 'h', 'e', 't'}\n"
     ]
    }
   ],
   "source": [
    "dw_set = set() # 集合\n",
    "for i in \"Datawhale\":\n",
    "    dw_set.add(i)\n",
    "print(dw_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "04eea9e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:18:26.267517Z",
     "start_time": "2023-07-22T12:18:26.249517Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Datawhale\n"
     ]
    }
   ],
   "source": [
    "dw_dict = {\"d\":\"Data\", \"w\":\"whale\"} # key:value \n",
    "print(dw_dict[\"d\"], dw_dict[\"w\"], sep=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9023602e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-13T04:47:16.317031Z",
     "start_time": "2023-07-13T04:47:16.293190Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "字典的更新"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c381c088",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:19:57.557137Z",
     "start_time": "2023-07-22T12:19:57.547139Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'d': 'Data', 'w': 'Whale'}\n"
     ]
    }
   ],
   "source": [
    "dw_dict[\"w\"] = \"Whale\"\n",
    "print(dw_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a2d807c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# [if](https://www.runoob.com/python3/python3-conditional-statements.html#:~:text=%E7%9A%84%E6%89%A7%E8%A1%8C%E8%BF%87%E7%A8%8B%3A-,%E4%BB%A3%E7%A0%81%E6%89%A7%E8%A1%8C%E8%BF%87%E7%A8%8B%EF%BC%9A,-if%20%E8%AF%AD%E5%8F%A5) and [for](https://www.runoob.com/python3/python3-loop.html#:~:text=%3A%0A%20%20%20%20%3Cstatements%3E-,%E6%B5%81%E7%A8%8B%E5%9B%BE%EF%BC%9A,-Python%20for%20%E5%BE%AA%E7%8E%AF)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f05a03b",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "> 女朋友说，下班回来带一个西瓜。如果看到番茄，就买两个 \n",
    "\n",
    "[来源：知乎](https://www.zhihu.com/question/45194583)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "fb6902fe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:24:35.095481Z",
     "start_time": "2023-07-22T12:24:35.086482Z"
    },
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "def 买西瓜(num):\n",
    "    return f\"{num}个西瓜\"\n",
    "def 买番茄(num):\n",
    "    return f\"{num}个番茄\"\n",
    "\n",
    "看到番茄 = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "5cf27a89",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:25:09.575335Z",
     "start_time": "2023-07-22T12:25:09.558334Z"
    },
    "cell_style": "split",
    "run_control": {
     "marked": false
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "带了2个西瓜回家\n"
     ]
    }
   ],
   "source": [
    "# 下班了\n",
    "西瓜 = 买西瓜(1)\n",
    "if 看到番茄:\n",
    "    西瓜 = 买西瓜(2)\n",
    "print(f\"带了{西瓜}回家\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "566b53f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:25:35.121014Z",
     "start_time": "2023-07-22T12:25:35.113013Z"
    },
    "cell_style": "split",
    "run_control": {
     "marked": false
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "带了1个西瓜和2个番茄回家\n"
     ]
    }
   ],
   "source": [
    "# 女朋友心想\n",
    "西瓜 = 买西瓜(1)\n",
    "if 看到番茄:\n",
    "    番茄 = 买番茄(2)\n",
    "print(f\"带了{西瓜}和{番茄}回家\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "38925f7d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:27:06.578510Z",
     "start_time": "2023-07-22T12:27:06.561508Z"
    },
    "cell_style": "split",
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🐳🐳🐳\n",
      "🐳🐳🐳\n",
      "🐳🐳🐳\n"
     ]
    }
   ],
   "source": [
    "for row in range(3):\n",
    "    for col in range(3):\n",
    "        print(\"🐳\", end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e2bc3e8b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:29:46.759041Z",
     "start_time": "2023-07-22T12:29:46.749039Z"
    },
    "cell_style": "split",
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "learn\n",
      "python\n",
      "the\n",
      "smart\n",
      "way\n"
     ]
    }
   ],
   "source": [
    "for string in \"learn python the smart way\".split():\n",
    "    print(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "6eb860cf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:30:20.860759Z",
     "start_time": "2023-07-22T12:30:20.845748Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['我', '秦始皇', 'V50', '吃肯德基']\n"
     ]
    }
   ],
   "source": [
    "print(\"我，秦始皇，V50，吃肯德基\".split(\"，\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d516eb3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 自定义函数 与 return"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c70991b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> A function is a procedure (a sequence of statements) stored under a name that can be used repeatedly by calling the name."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60621952",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "以 **Rectified Linear Unit (ReLU)** 函数为例，其数学表达式为：\n",
    "\n",
    "$f(x) = \\begin{cases}\n",
    "    x, x > 0, \\\\\n",
    "    0, x \\leqslant 0.\n",
    "\\end{cases}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "e38903f5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:33:09.172543Z",
     "start_time": "2023-07-22T12:33:09.163546Z"
    },
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "def ReLU(x): # f(x)\n",
    "    if x > 0:\n",
    "        return x\n",
    "    return 0\n",
    "\n",
    "print(ReLU(9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "67ebb5f5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:34:15.424080Z",
     "start_time": "2023-07-22T12:34:15.418075Z"
    },
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "relu = lambda x: max(x, 0)\n",
    "print(relu(-9))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67c95b8f",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## 回调函数\n",
    "[回调函数（callback）是什么？ - 常溪玲的回答 - 知乎](https://www.zhihu.com/question/19801131/answer/13005983)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5c47079",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 一种基于深度抄袭的机器学习时间特征提取技术"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f5ff730",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T08:23:01.469151Z",
     "start_time": "2023-07-21T08:23:01.099031Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "直接赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c44c9a6b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:36:53.228648Z",
     "start_time": "2023-07-22T12:36:53.204649Z"
    },
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, 2442188747328)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3, [4, 5]]\n",
    "b = a\n",
    "id(a) == id(b), id(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d33b76b",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "浅度抄袭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ad933356",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:37:40.674124Z",
     "start_time": "2023-07-22T12:37:40.668125Z"
    },
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b: [1, 2, 3, [4, 5], 6]\n",
      "c: [1, 2, 3, [4, 5]]\n"
     ]
    }
   ],
   "source": [
    "c = a.copy() # a:v1.0\n",
    "a.append(6) # a:2.0\n",
    "print(f\"b: {b}\\nc: {c}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "8cd42ab0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:39:06.026962Z",
     "start_time": "2023-07-22T12:39:06.012966Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a[3] => [4, 5]\n",
      "\n",
      "b: [1, 2, 3, [4, 5, 7], 6]\n",
      "c: [1, 2, 3, [4, 5, 7]]\n"
     ]
    }
   ],
   "source": [
    "print(f\"a[3] => {a[3]}\\n\")\n",
    "a[3].append(7) # a:3.0\n",
    "print(f\"b: {b}\\nc: {c}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8670d23f",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "深度抄袭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "0f299052",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:41:38.906051Z",
     "start_time": "2023-07-22T12:41:38.891053Z"
    },
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import copy\n",
    "\n",
    "d = copy.deepcopy(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "e4573acb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:42:03.288416Z",
     "start_time": "2023-07-22T12:42:03.272900Z"
    },
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b: [1, 2, 3, [4, 5, 7, 8], 6]\n",
      "c: [1, 2, 3, [4, 5, 7, 8]]\n",
      "d: [1, 2, 3, [4, 5, 7], 6]\n"
     ]
    }
   ],
   "source": [
    "a[3].append(8)\n",
    "print(f\"b: {b}\\nc: {c}\\nd: {d}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5173404",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-13T06:30:12.381192Z",
     "start_time": "2023-07-13T06:30:12.378186Z"
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 托马斯小火车与它的对象们"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "68d2185f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-23T04:31:55.176735Z",
     "start_time": "2023-07-23T04:31:55.168736Z"
    },
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "from random import choice\n",
    "import time\n",
    "from tqdm import tqdm\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "class Train:\n",
    "    def __init__(self, name, *goods, naughty=True):\n",
    "        self.name = name\n",
    "        self.goods = goods\n",
    "        self.naughty = naughty\n",
    "    \n",
    "    def __getitem__(self, idx):\n",
    "        return self.goods[idx]\n",
    "    \n",
    "    def __len__(self):\n",
    "        return len(self.goods)\n",
    "    \n",
    "    def info(self):\n",
    "        if self.name == \"托马斯小火车\":\n",
    "            return f\"Hi，我是{self.name}，Cinders and ashes!\"\n",
    "        return f\"Hi，我是{self.name}\"\n",
    "    \n",
    "    def 发车(self, string):\n",
    "        print(f\"{string}，上山！\") # f-strings\n",
    "        for _ in tqdm(range(30)):\n",
    "            time.sleep(0.1)\n",
    "        display(HTML(\"<video controls width=1200 src='train.mp4'>train</video>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "a1e8ec1f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:48:04.389129Z",
     "start_time": "2023-07-22T12:48:04.378129Z"
    },
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "Edward = Train(\"爱德华\", 1,  2.5, 9, 114, 514, naughty=False) # 实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "14d1c19b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:48:16.184810Z",
     "start_time": "2023-07-22T12:48:16.172806Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hi，我是爱德华'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Edward.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "f532074d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:49:13.544788Z",
     "start_time": "2023-07-22T12:49:13.526787Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(Edward)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "d29a3fc6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:52:42.091056Z",
     "start_time": "2023-07-22T12:52:42.084055Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello 514\n"
     ]
    }
   ],
   "source": [
    "a, b= Edward[3], Edward[4] # 解包与打包 (114, 514) => a, b\n",
    "print(f\"hello {b}\") # f-strings "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "b215d3d3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-23T04:31:59.484440Z",
     "start_time": "2023-07-23T04:31:59.469924Z"
    },
    "run_control": {
     "marked": false
    },
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hi，我是托马斯小火车，Cinders and ashes!'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "货物 = [\"邮件\", \"大米\", \"煤矿\", \"劳斯莱斯\"]\n",
    "托马斯 = Train(\"托马斯小火车\", *货物, naughty=True)\n",
    "托马斯.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "87f58c0a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:53:18.569728Z",
     "start_time": "2023-07-22T12:53:18.561212Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(托马斯)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "781d99bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T12:53:41.100392Z",
     "start_time": "2023-07-22T12:53:41.089391Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'煤矿'"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "托马斯[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "bb788cf8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-23T04:32:06.262208Z",
     "start_time": "2023-07-23T04:32:02.945747Z"
    },
    "scrolled": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AE86，上山！\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 30/30 [00:03<00:00,  9.08it/s]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<video controls width=1200 src='train.mp4'>train</video>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "托马斯.发车(\"AE86\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc14897b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Thank You ;-)\n",
    "Datawhale 聪明办法学 Python 教学团队出品\n",
    "\n",
    "## 关注我们\n",
    "Datawhale 是一个专注 AI 领域的开源组织，以“for the learner，和学习者一起成长”为愿景，构建对学习者最有价值的开源学习社区。关注我们，一起学习成长。\n",
    "<div align=center><img src=\"datawhale_wechat_qrcode.jpeg\" width = \"250\" height = \"270\"></div>"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "rise": {
   "overlay": "<div class='my-top-right'><img height=40px src='datawhale_logo.png'/></div><div class='my-top-left'></div>"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
